package com.mfnote.view.setting;

import com.mfnote.view.R;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.R.style;
import com.mfnote.view.main.MainActivity;
import com.mfnote.view.other.MyApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class EncyptActivity extends Activity implements OnClickListener {

	Button btn_openEncypt, btn_cancelEncypt; // 启用加密，取消加密
	EditText encypt_et_1, encypt_et_2; // 密码输入框
	ImageButton iv_back;  //返回
	
	Dialog cancelEncryptDlg; //输入密码对话框
	private String isEncypt = "";//获取配置文件中的密码
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.encypt);
		
		MyApplication.getInstance().addActivity(this);

		// 加载控件
		btn_openEncypt = (Button) this.findViewById(R.id.btn_openEncypt);
		btn_cancelEncypt = (Button) this.findViewById(R.id.btn_cancelEncypt);
		encypt_et_1 = (EditText) this.findViewById(R.id.encypt_et_1);
		encypt_et_2 = (EditText) this.findViewById(R.id.encypt_et_2);
		iv_back = (ImageButton) this.findViewById(R.id.about_back);
		
		// 绑定监听器
		btn_openEncypt.setOnClickListener(this);
		btn_cancelEncypt.setOnClickListener(this);
		iv_back.setOnClickListener(this);
		
		//获取配置文件信息
		SharedPreferences sharedPreferences = getSharedPreferences(
				"LoginValue", Context.MODE_WORLD_READABLE);
		// getString()第二个参数为缺省值，如果preference中不存在该key，将返回缺省值
		isEncypt = sharedPreferences.getString("isEncypt", "false");
		
	}

	/** 启用加密操作 **/
	private void openEncypt() {
		//接收数据
		String password1 = encypt_et_1.getText().toString();
		String password2 = encypt_et_2.getText().toString();
		if(!password1.equals(password2)){
			Toast.makeText(EncyptActivity.this, "两次密码不一致请重新输入！", Toast.LENGTH_SHORT).show();
		}else{
			if(password1.equals("") && password2.equals("")) {
				Toast.makeText(EncyptActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
			} else {
//				
				//用SharedPrefrence
				SharedPreferences sharedPreferences = getSharedPreferences("LoginValue",Context.MODE_WORLD_WRITEABLE);
				Editor editor = sharedPreferences.edit();//获取编辑器
				editor.putString("password", password1);  //将加密过的密码保存起来
				editor.putString("isEncypt", "true");
				editor.commit();//提交修改
				
				Toast.makeText(EncyptActivity.this, "密码保存成功！", Toast.LENGTH_SHORT).show();
				Intent intent  = new Intent(EncyptActivity.this,OpenEncyptActivity.class);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.push_right_in,
						R.anim.push_left_out);

			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_openEncypt: // 启用加密操作
			openEncypt();
			break;
		case R.id.btn_cancelEncypt: //取消加密操作
			if(isEncypt.equals("false")){
				Toast.makeText(this, "当前未设置密码", Toast.LENGTH_SHORT).show();
			}else{
				cancelEncypt();
			}
			
			break;
		case R.id.about_back:
			Intent intent = new Intent(EncyptActivity.this,SettingActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_left_in,
					R.anim.push_right_out);
			break;
		}
	}

	/**取消加密**/
	private void cancelEncypt() {		
		View view = getLayoutInflater().from(EncyptActivity.this).inflate(
				R.layout.encrypt_dialog, null);
		final EditText editText = (EditText) view
				.findViewById(R.id.editText);
		
		cancelEncryptDlg = new Dialog(this, R.style.login_dialog_Dialog);
		cancelEncryptDlg.setContentView(view);
		
		Button sure_btn = (Button) view.findViewById(R.id.sure_btn);
		Button cancel_btn = (Button) view.findViewById(R.id.cancel_btn);
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String temPass = editText.getText().toString();
				//获取配置文件信息
				SharedPreferences sharedPreferences = getSharedPreferences(
						"LoginValue", Context.MODE_WORLD_READABLE);
				// getString()第二个参数为缺省值，如果preference中不存在该key，将返回缺省值
				String password = sharedPreferences.getString("password", "");
				
				if (password.equals(temPass)) {  
					//正确输入密码

				 //用SharedPrefrence
				  SharedPreferences spf = getSharedPreferences("LoginValue",Context.MODE_WORLD_WRITEABLE);
				  Editor editor = spf.edit();//获取编辑器
				  editor.putString("isEncypt", "false");  //将其存入配置文件
				  editor.commit();//提交修改
				  
				  Toast.makeText(EncyptActivity.this, "已成功取消加密！", Toast.LENGTH_SHORT).show();
				  
				  Intent intent  = new Intent(EncyptActivity.this,SettingActivity.class);
				  startActivity(intent);
				  finish();
				  overridePendingTransition(R.anim.push_left_in,
							R.anim.push_right_out);
			}else{
				Toast.makeText(EncyptActivity.this, "密码输入错误，取消加密失败！", Toast.LENGTH_SHORT).show();
				cancelEncryptDlg.dismiss(); 
			}
		 }
		});
		//取消
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				// 弹出Exit对话框
				cancelEncryptDlg.dismiss();
			}
		});
		
		cancelEncryptDlg.show();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this,SettingActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
		}
		return true;
	}

}
